﻿/*
 * Jsofteditor - The text editor for Internet - http://www.jsofteditor.net
 * Copyright (C) 2003-2009 Frederico Caldeira Knabben
 *
 * == BEGIN LICENSE ==
 *
 * Licensed under the terms of any of the following licenses at your
 * choice:
 *
 *  - GNU General Public License Version 2 or later (the "GPL")
 *    http://www.gnu.org/licenses/gpl.html
 *
 *  - GNU Lesser General Public License Version 2.1 or later (the "LGPL")
 *    http://www.gnu.org/licenses/lgpl.html
 *
 *  - Mozilla Public License Version 1.1 or later (the "MPL")
 *    http://www.mozilla.org/MPL/MPL-1.1.html
 *
 * == END LICENSE ==
 *
 * Define all commands available in the editor.
 */

var JsoftCommands = Jsoft.Commands = new Object() ;
JsoftCommands.LoadedCommands = new Object() ;

JsoftCommands.RegisterCommand = function( commandName, command )
{
	this.LoadedCommands[ commandName ] = command ;
}

JsoftCommands.GetCommand = function( commandName )
{
	var oCommand = JsoftCommands.LoadedCommands[ commandName ] ;

	if ( oCommand )
		return oCommand ;

	switch ( commandName )
	{
		case 'Bold'			:
		case 'Italic'		:
		case 'Underline'	:
		case 'StrikeThrough':
		case 'Subscript'	:
		case 'Superscript'	: oCommand = new JsoftCoreStyleCommand( commandName ) ; break ;

		case 'RemoveFormat'	: oCommand = new JsoftRemoveFormatCommand() ; break ;

		case 'DocProps'		: oCommand = new JsoftDialogCommand( 'DocProps'	, JsoftLang.DocProps				, 'dialog/jsoft_docprops.html'	, 400, 380, JsoftCommands.GetFullPageState ) ; break ;
		case 'Templates'	: oCommand = new JsoftDialogCommand( 'Templates'	, JsoftLang.DlgTemplatesTitle		, 'dialog/jsoft_template.html'	, 380, 450 ) ; break ;
		case 'Link'			: oCommand = new JsoftDialogCommand( 'Link'		, JsoftLang.DlgLnkWindowTitle		, 'dialog/jsoft_link.html'		, 400, 300 ) ; break ;
		case 'Unlink'		: oCommand = new JsoftUnlinkCommand() ; break ;
		case 'VisitLink'	: oCommand = new JsoftVisitLinkCommand() ; break ;
		case 'Anchor'		: oCommand = new JsoftDialogCommand( 'Anchor'		, JsoftLang.DlgAnchorTitle		, 'dialog/jsoft_anchor.html'		, 370, 160 ) ; break ;
		case 'AnchorDelete'	: oCommand = new JsoftAnchorDeleteCommand() ; break ;
		case 'BulletedList'	: oCommand = new JsoftDialogCommand( 'BulletedList', JsoftLang.BulletedListProp		, 'dialog/jsoft_listprop.html?UL'	, 370, 160 ) ; break ;
		case 'NumberedList'	: oCommand = new JsoftDialogCommand( 'NumberedList', JsoftLang.NumberedListProp		, 'dialog/jsoft_listprop.html?OL'	, 370, 160 ) ; break ;
		case 'About'		: oCommand = new JsoftDialogCommand( 'About'		, JsoftLang.About					, 'dialog/jsoft_about.html'		, 420, 330, function(){ return Jsoft_TRISTATE_OFF ; } ) ; break ;
		case 'Find'			: oCommand = new JsoftDialogCommand( 'Find'		, JsoftLang.DlgFindAndReplaceTitle, 'dialog/jsoft_replace.html'		, 340, 230, null, null, 'Find' ) ; break ;
		case 'Replace'		: oCommand = new JsoftDialogCommand( 'Replace'	, JsoftLang.DlgFindAndReplaceTitle, 'dialog/jsoft_replace.html'		, 340, 230, null, null, 'Replace' ) ; break ;

		case 'Image'		: oCommand = new JsoftDialogCommand( 'Image'		, JsoftLang.DlgImgTitle			, 'dialog/jsoft_image.html'		, 450, 390 ) ; break ;
		case 'Flash'		: oCommand = new JsoftDialogCommand( 'Flash'		, JsoftLang.DlgFlashTitle			, 'dialog/jsoft_flash.html'		, 450, 390 ) ; break ;
		case 'SpecialChar'	: oCommand = new JsoftDialogCommand( 'SpecialChar', JsoftLang.DlgSpecialCharTitle	, 'dialog/jsoft_specialchar.html'	, 400, 290 ) ; break ;
		case 'Smiley'		: oCommand = new JsoftDialogCommand( 'Smiley'		, JsoftLang.DlgSmileyTitle		, 'dialog/jsoft_smiley.html'		, JsoftConfig.SmileyWindowWidth, JsoftConfig.SmileyWindowHeight ) ; break ;
		case 'Table'		: oCommand = new JsoftDialogCommand( 'Table'		, JsoftLang.DlgTableTitle			, 'dialog/jsoft_table.html'		, 480, 250 ) ; break ;
		case 'TableProp'	: oCommand = new JsoftDialogCommand( 'Table'		, JsoftLang.DlgTableTitle			, 'dialog/jsoft_table.html?Parent', 480, 250 ) ; break ;
		case 'TableCellProp': oCommand = new JsoftDialogCommand( 'TableCell'	, JsoftLang.DlgCellTitle			, 'dialog/jsoft_tablecell.html'	, 550, 240 ) ; break ;

		case 'Style'		: oCommand = new JsoftStyleCommand() ; break ;

		case 'FontName'		: oCommand = new JsoftFontNameCommand() ; break ;
		case 'FontSize'		: oCommand = new JsoftFontSizeCommand() ; break ;
		case 'FontFormat'	: oCommand = new JsoftFormatBlockCommand() ; break ;

		case 'Source'		: oCommand = new JsoftSourceCommand() ; break ;
		case 'Preview'		: oCommand = new JsoftPreviewCommand() ; break ;
		case 'Save'			: oCommand = new JsoftSaveCommand() ; break ;
		case 'NewPage'		: oCommand = new JsoftNewPageCommand() ; break ;
		case 'PageBreak'	: oCommand = new JsoftPageBreakCommand() ; break ;
		case 'Rule'			: oCommand = new JsoftRuleCommand() ; break ;
		case 'Nbsp'			: oCommand = new JsoftNbsp() ; break ;

		case 'TextColor'	: oCommand = new JsoftTextColorCommand('ForeColor') ; break ;
		case 'BGColor'		: oCommand = new JsoftTextColorCommand('BackColor') ; break ;

		case 'Paste'		: oCommand = new JsoftPasteCommand() ; break ;
		case 'PasteText'	: oCommand = new JsoftPastePlainTextCommand() ; break ;
		case 'PasteWord'	: oCommand = new JsoftPasteWordCommand() ; break ;

		case 'JustifyLeft'	: oCommand = new JsoftJustifyCommand( 'left' ) ; break ;
		case 'JustifyCenter'	: oCommand = new JsoftJustifyCommand( 'center' ) ; break ;
		case 'JustifyRight'	: oCommand = new JsoftJustifyCommand( 'right' ) ; break ;
		case 'JustifyFull'	: oCommand = new JsoftJustifyCommand( 'justify' ) ; break ;
		case 'Indent'	: oCommand = new JsoftIndentCommand( 'indent', JsoftConfig.IndentLength ) ; break ;
		case 'Outdent'	: oCommand = new JsoftIndentCommand( 'outdent', JsoftConfig.IndentLength * -1 ) ; break ;
		case 'Blockquote'	: oCommand = new JsoftBlockQuoteCommand() ; break ;
		case 'CreateDiv'	: oCommand = new JsoftDialogCommand( 'CreateDiv', JsoftLang.CreateDiv, 'dialog/jsoft_div.html', 380, 210, null, null, true ) ; break ;
		case 'EditDiv'		: oCommand = new JsoftDialogCommand( 'EditDiv', JsoftLang.EditDiv, 'dialog/jsoft_div.html', 380, 210, null, null, false ) ; break ;
		case 'DeleteDiv'	: oCommand = new JsoftDeleteDivCommand() ; break ;

		case 'TableInsertRowAfter'		: oCommand = new JsoftTableCommand('TableInsertRowAfter') ; break ;
		case 'TableInsertRowBefore'		: oCommand = new JsoftTableCommand('TableInsertRowBefore') ; break ;
		case 'TableDeleteRows'			: oCommand = new JsoftTableCommand('TableDeleteRows') ; break ;
		case 'TableInsertColumnAfter'	: oCommand = new JsoftTableCommand('TableInsertColumnAfter') ; break ;
		case 'TableInsertColumnBefore'	: oCommand = new JsoftTableCommand('TableInsertColumnBefore') ; break ;
		case 'TableDeleteColumns'		: oCommand = new JsoftTableCommand('TableDeleteColumns') ; break ;
		case 'TableInsertCellAfter'		: oCommand = new JsoftTableCommand('TableInsertCellAfter') ; break ;
		case 'TableInsertCellBefore'	: oCommand = new JsoftTableCommand('TableInsertCellBefore') ; break ;
		case 'TableDeleteCells'			: oCommand = new JsoftTableCommand('TableDeleteCells') ; break ;
		case 'TableMergeCells'			: oCommand = new JsoftTableCommand('TableMergeCells') ; break ;
		case 'TableMergeRight'			: oCommand = new JsoftTableCommand('TableMergeRight') ; break ;
		case 'TableMergeDown'			: oCommand = new JsoftTableCommand('TableMergeDown') ; break ;
		case 'TableHorizontalSplitCell'	: oCommand = new JsoftTableCommand('TableHorizontalSplitCell') ; break ;
		case 'TableVerticalSplitCell'	: oCommand = new JsoftTableCommand('TableVerticalSplitCell') ; break ;
		case 'TableDelete'				: oCommand = new JsoftTableCommand('TableDelete') ; break ;

		case 'Form'			: oCommand = new JsoftDialogCommand( 'Form'		, JsoftLang.Form			, 'dialog/jsoft_form.html'		, 380, 210 ) ; break ;
		case 'Checkbox'		: oCommand = new JsoftDialogCommand( 'Checkbox'	, JsoftLang.Checkbox		, 'dialog/jsoft_checkbox.html'	, 380, 200 ) ; break ;
		case 'Radio'		: oCommand = new JsoftDialogCommand( 'Radio'		, JsoftLang.RadioButton	, 'dialog/jsoft_radiobutton.html'	, 380, 200 ) ; break ;
		case 'TextField'	: oCommand = new JsoftDialogCommand( 'TextField'	, JsoftLang.TextField		, 'dialog/jsoft_textfield.html'	, 380, 210 ) ; break ;
		case 'Textarea'		: oCommand = new JsoftDialogCommand( 'Textarea'	, JsoftLang.Textarea		, 'dialog/jsoft_textarea.html'	, 380, 210 ) ; break ;
		case 'HiddenField'	: oCommand = new JsoftDialogCommand( 'HiddenField', JsoftLang.HiddenField	, 'dialog/jsoft_hiddenfield.html'	, 380, 190 ) ; break ;
		case 'Button'		: oCommand = new JsoftDialogCommand( 'Button'		, JsoftLang.Button		, 'dialog/jsoft_button.html'		, 380, 210 ) ; break ;
		case 'Select'		: oCommand = new JsoftDialogCommand( 'Select'		, JsoftLang.SelectionField, 'dialog/jsoft_select.html'		, 400, 340 ) ; break ;
		case 'ImageButton'	: oCommand = new JsoftDialogCommand( 'ImageButton', JsoftLang.ImageButton	, 'dialog/jsoft_image.html?ImageButton', 450, 390 ) ; break ;

		case 'SpellCheck'	: oCommand = new JsoftSpellCheckCommand() ; break ;
		case 'FitWindow'	: oCommand = new JsoftFitWindow() ; break ;

		case 'Undo'	: oCommand = new JsoftUndoCommand() ; break ;
		case 'Redo'	: oCommand = new JsoftRedoCommand() ; break ;
		case 'Copy'	: oCommand = new JsoftCutCopyCommand( false ) ; break ;
		case 'Cut'	: oCommand = new JsoftCutCopyCommand( true ) ; break ;

		case 'SelectAll'			: oCommand = new JsoftSelectAllCommand() ; break ;
		case 'InsertOrderedList'	: oCommand = new JsoftListCommand( 'insertorderedlist', 'ol' ) ; break ;
		case 'InsertUnorderedList'	: oCommand = new JsoftListCommand( 'insertunorderedlist', 'ul' ) ; break ;
		case 'ShowBlocks' : oCommand = new JsoftShowBlockCommand( 'ShowBlocks', JsoftConfig.StartupShowBlocks ? Jsoft_TRISTATE_ON : Jsoft_TRISTATE_OFF ) ; break ;

		// Generic Undefined command (usually used when a command is under development).
		case 'Undefined'	: oCommand = new JsoftUndefinedCommand() ; break ;

		// By default we assume that it is a named command.
		default:
			if ( JsoftRegexLib.NamedCommands.test( commandName ) )
				oCommand = new JsoftNamedCommand( commandName ) ;
			else
			{
				alert( JsoftLang.UnknownCommand.replace( /%1/g, commandName ) ) ;
				return null ;
			}
	}

	JsoftCommands.LoadedCommands[ commandName ] = oCommand ;

	return oCommand ;
}

// Gets the state of the "Document Properties" button. It must be enabled only
// when "Full Page" editing is available.
JsoftCommands.GetFullPageState = function()
{
	return JsoftConfig.FullPage ? Jsoft_TRISTATE_OFF : Jsoft_TRISTATE_DISABLED ;
}


JsoftCommands.GetBooleanState = function( isDisabled )
{
	return isDisabled ? Jsoft_TRISTATE_DISABLED : Jsoft_TRISTATE_OFF ;
}
